//
//  LXRescindViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/13.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXRescindViewController.h"
#import "LXSignBlockViewController.h"
@interface LXRescindViewController ()
@property (nonatomic) UIButton *selBtn;
@property (nonatomic, copy) NSString *rescindReason;
@property (nonatomic, copy) NSString *rescindRemark;
@property (nonatomic, strong) PlaceholderTextView *textView;

@end

@implementation LXRescindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self layoutCustomNav:self.view title:@"到期解约"];
    
    
    [self setBottomView];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - 50)];
    [self.view addSubview:scrollView];
  
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 100, 20)];
    lab.text = @"解约原因";
    lab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    lab.textColor = [UIColor colorWithHexString:@"#222222"];
    [scrollView addSubview:lab];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, lab.Y_height + 10, ScreenWidth, 0)];
    view.backgroundColor = UIColor.whiteColor;
    [scrollView addSubview:view];
   
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *json = @"RESCIND_REASON";
    
    [params setObject:json forKey:@"code"];
    NSString *url = [NSString stringWithFormat:@"%@?code=%@",CarDetailDict,json];
    [MSNetworkManager postRevertUrl:url params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            CGFloat W = (ScreenWidth - 80) / 3;
            CGFloat M = 20;
            CGFloat H = 36;
            
            NSArray *ary = (NSArray *)resonseObj;
            
            
            CGFloat height = ary.count/3 * H;
            if (ary.count%3 !=0 ){
                height = (ary.count/3 +1) * H ;
            }
            view.height = height + 30;
            
            
            for (int i = 0; i < [ary count]; i++) {

                NSDictionary *dic = ary[i];
                
                
                NSInteger row = i/3;
                NSInteger col = i%3;
                
                
                CGFloat X =  col  * (W + M) + M;
                CGFloat Y =  row * (H + 10);
                
                
                
                UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
                [btn setTitleColor:UIColor.lightGrayColor forState:(UIControlStateNormal)];
                [btn setTitleColor:UIColor.whiteColor forState:(UIControlStateSelected)];
                btn.frame = CGRectMake(X, Y, W, H);
                [btn addCorner:4];
                btn.tag = 3333 + i;
                btn.backgroundColor = ColorWithF5F5F5;
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
                [btn setTitle:dic[@"value"] forState:(UIControlStateNormal)];
                [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
                [view addSubview:btn];
                
            }
            
            
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, view.Y_height, 100, 20)];
            lab.text = @"解约备注";
            lab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
            lab.textColor = [UIColor colorWithHexString:@"#222222"];
            [scrollView addSubview:lab];
            
            
            
        
            self.textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(15, lab.Y_height + 15, ScreenWidth - 30, 130)];
            [self.textView addCorner:5];
            self.textView.maxLength = 100;
            self.textView.wordNumLabel.text = @"0/100";
            self.textView.placeholderLabel.font = [UIFont systemFontOfSize:14];
            self.textView.placeholder = @"请输入投诉内容…";
            self.textView.backgroundColor = ColorWithF5F5F5;
            self.textView.textColor = [UIColor colorWithHexString:@"#222222"];
            self.textView.font = [UIFont systemFontOfSize:14];
            WEAKSELF
            [self.textView setDidChangeText:^(PlaceholderTextView *textView) {
                weakSelf.rescindRemark = textView.text;
            }];
            [scrollView addSubview:self.textView];
            
            
          
            
            
            
            scrollView.contentSize = CGSizeMake(ScreenWidth, 0);
            
        }

    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:NO];
    
    
    
}
-(void)btnAction:(UIButton *)sender{
    
    self.selBtn.selected = NO;
    [self.selBtn setBackgroundColor:ColorWithF5F5F5];
    
    sender.selected = YES;
    [sender setBackgroundColor:mainColor];
    self.rescindReason = sender.titleLabel.text;
    self.selBtn = sender;
    
}

-(void)setBottomView{

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, kScreenHeight - kComHeight, ScreenWidth - 30, kComHeight)];
    btn.backgroundColor = mainColor;
    [btn addCorner:4];
    [btn setTitle:@"确认解约" forState:UIControlStateNormal];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    btn.titleLabel.font = Fond16;
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}

-(void)click{
    
    
    
  
    
    [Tool showAlertTitle:@"温馨提示" msg:@"确认解约" cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        
        NSDictionary *dic = @{
            @"signContractId":self.signContractId,
            @"rescindReason":self.rescindReason,
            @"rescindRemark":self.rescindRemark,
        };
        [MSNetworkManager postRevertUrl:POST_Rescind params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                
                kUserDefaultSetBool(YES, kRefreshStr);
                
                LXSignBlockViewController *vc = [[LXSignBlockViewController alloc] init];
                vc.type = 4;
                vc.fromVC = self.fromVC;
                [self pushToNextController:vc];
                
                
            }

        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:NO];

        
        
    } onVC:self];

    
    
    
    
    
    
}
@end
